package com.patterns.mediator;

import java.util.ArrayList;
import java.util.List;

/**
 * 具体中介者
 * @author fwj
 * @date 2021/4/21 16:47
 **/
public class ConcreteMediator implements Mediator{

    private List<Colleague> colleagues = new ArrayList<Colleague>();

    /**
     * 注册
     * @param colleague
     */
    public void register(Colleague colleague) {
        if (!colleagues.contains(colleague)) {
            colleagues.add(colleague);
            colleague.setMedium(this);
        }
    }

    /**
     * 具体同事类发生变化时，通知其他
     * @param colleague
     */
    public void relay(Colleague cl) {
        for (Colleague ob : colleagues) {
            if (!ob.equals(cl)) {
                ((Colleague) ob).receive();
            }
        }
    }

}
